package com.pig4cloud.pig.buyan.controller;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.buyan.entity.Style;
import com.pig4cloud.pig.buyan.service.StyleService;
import com.pig4cloud.pig.common.core.util.R;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.http.HttpHeaders;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;


@RestController
@RequiredArgsConstructor
@RequestMapping("/style")
@Tag(description = "style", name = "汽车款式模块")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class StyleController {

	private final StyleService styleService;
	//添加功能
	@PostMapping("/insert")
	public R save(@RequestBody Style style){
		styleService.save(style);
		return R.ok("添加成功");
	}
	//分页查询
	@GetMapping("/page")
	public R getStylePage(@ParameterObject Page page, @ParameterObject Style style) {
		LambdaQueryWrapper<Style> wrapper = Wrappers.<Style>lambdaQuery()
				.eq(StrUtil.isNotBlank(style.getDelFlag()),Style::getDelFlag,style.getDelFlag());
		return R.ok(styleService.page(page, wrapper));
	}

	//更新功能
	@PutMapping("/edit")
	public R updateById(@RequestBody Style style){
		if(styleService.getById(style.getStyleId())!=null){
			return R.ok(styleService.updateById(style));
		}else {
			return R.failed("更新失败");
		}
	}
	//逻辑删除
	@DeleteMapping("/del/{ids}")
	public R removeById(@PathVariable Long[] ids){
		return R.ok(styleService.removeBatchByIds(CollUtil.toList(ids)));
	}
	//根据id查询具体数据
	@GetMapping("/select/{styleId}")
	public R getById(@PathVariable("styleId") Long styleId){
		return R.ok(styleService.getById(styleId));
	}

}

